package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNReceiverInfo;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.bean.YnRedEnvelopInfo;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.UIHelper;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.util.LinkedHashMap;

public class PailideShowHongbaoActivity extends Activity implements View.OnClickListener {
    private ImageView show_cover;
    private RelativeLayout time_pailide;
    private TextView redtotalnum;
    private TextView redremainnum;
    private YnRedEnvelopInfo ynRedEnvelopInfo;
    private YNStatus ynStatus;
    private BaseApplication application;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_show_pailidehongbao);
        ynStatus = YNSingleton.getInstance().getCurStatus();
        ynRedEnvelopInfo = ynStatus.getRedEnvelopInfo();
        application = (BaseApplication) getApplication();
        initView();
    }

    private void initView() {
        findViewById(R.id.pailideshow_hongbao_close).setOnClickListener(this);
        findViewById(R.id.pailideshow_hongbao_open).setOnClickListener(this);
        show_cover = (ImageView) findViewById(R.id.show_cover);
        time_pailide = (RelativeLayout) findViewById(R.id.time_pailide);
        AlphaAnimation alphaAnimationbtns = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimationbtns.setDuration(800);
        show_cover.startAnimation(alphaAnimationbtns);
        Animation translateAnimationf = new TranslateAnimation(0, 0, -UIHelper.dip2px(PailideShowHongbaoActivity.this, 220), 0);
        translateAnimationf.setDuration(500);
        time_pailide.startAnimation(translateAnimationf);
        redtotalnum = (TextView) findViewById(R.id.redtotalnum);
        redremainnum = (TextView) findViewById(R.id.redremainnum);
        redtotalnum.setText(String.valueOf(ynRedEnvelopInfo.getRedEnvelopTotalNum()));
        redremainnum.setText("还剩" + String.valueOf(ynRedEnvelopInfo.getRedEnvelopRemainNum()) + "个");
    }

    @Override
    public void onBackPressed() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pailideshow_hongbao_close:
                AlphaAnimation alphaAnimationbtns = new AlphaAnimation(1.0f, 0.0f);
                alphaAnimationbtns.setDuration(500);
                show_cover.startAnimation(alphaAnimationbtns);
                alphaAnimationbtns.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        show_cover.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                Animation translateAnimationf = new TranslateAnimation(0, 0, 0, UIHelper.dip2px(PailideShowHongbaoActivity.this, 220));
                translateAnimationf.setDuration(500);
                time_pailide.startAnimation(translateAnimationf);
                translateAnimationf.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        time_pailide.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
            case R.id.pailideshow_hongbao_open:
                GrabRedEnvelop();
                break;
        }
    }

    private YNReceiverInfo ynReceiverInfo;

    private void GrabRedEnvelop() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_REDENVELOPID, String.valueOf(ynRedEnvelopInfo.getRedEnvelopID()));
        Log.i("xjs", "getRedEnvelopID===>" + ynRedEnvelopInfo.getRedEnvelopID());
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/user/GrabRedEnvelop")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "GrabRedEnvelop===>" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.optInt("code") == 0) {
                                ynReceiverInfo = new YNReceiverInfo();
                                ynReceiverInfo.setUpic(jsonObject.optJSONArray("data").optJSONObject(0).
                                        optJSONArray("grabList").optJSONObject(0).optString("upic"));
                                ynReceiverInfo.setGrabMoney(jsonObject.optJSONArray("data").optJSONObject(0).
                                        optJSONArray("grabList").optJSONObject(0).optString("grabMoney"));
                                ynReceiverInfo.setGrabTime(jsonObject.optJSONArray("data").optJSONObject(0).
                                        optJSONArray("grabList").optJSONObject(0).optString("grabTime"));
                                ynReceiverInfo.setUserid(jsonObject.optJSONArray("data").optJSONObject(0).
                                        optJSONArray("grabList").optJSONObject(0).optInt("userid"));
                                ynReceiverInfo.setUnickname(jsonObject.optJSONArray("data").optJSONObject(0).
                                        optJSONArray("grabList").optJSONObject(0).optString("unickname"));
                                Intent openIntent = new Intent(PailideShowHongbaoActivity.this, PaiLidePaiHangActivity.class);
                                openIntent.putExtra("fromRedOpen", true);
                                openIntent.putExtra("ynReceiverInfo", ynReceiverInfo);
                                startActivity(openIntent);
                                finish();
                            } else {
                                Toast.makeText(PailideShowHongbaoActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(PailideShowHongbaoActivity.this, PaiLidePaiHangActivity.class));
                                finish();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(PailideShowHongbaoActivity.this, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(this)).executeAsync(req);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
